#include "Door.h"

Door::Door()
{
}

Door &Door::setCallback(DOOR_CALLBACK_SIGNATURE)
{
    this->callback = callback;
    return *this;
}

void Door::setup()
{
    pinMode(DOOR_ACTION_PIN, INPUT_PULLUP);
}

void Door::loop()
{
    // 100ms计数一次, 消除抖动
    long now = millis();
    if (now - last > 200)
    {
        last = now;
        // 读取引脚
        int value = getValue();
        // 状态变化
        if (value != _status)
        {
            _status = value;
            callback(getValue());
            return;
        }
    }
}

int Door::getValue()
{
    int value = digitalRead(DOOR_ACTION_PIN);
    if (value == LOW) // 低电平，门禁开启
    {
        return DOOR_ACTION_OPEN;
    }
    else 
    {
        return DOOR_ACTION_CLOSE;
    }
    return -1;
}
